//
//  BViewController.m
//  页面之间传值
//
//  Created by MAC on 2018/8/27.
//  Copyright © 2018年 MAC. All rights reserved.
//

#import "BViewController.h"
//导入B页面、单例类
#import "B-2-ViewController.h"
#import "DefaultInstance.h"

@interface BViewController ()
//声明全局指针，UILabelUIButton
@property (nonatomic,strong) UILabel *tsLabel;
@property (nonatomic,strong) UILabel *btLabel;
@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,strong) UILabel *tsLabel_01;
@end

@implementation BViewController

//UILabel的懒加载
- (UILabel *)tsLabel
{
    if (_tsLabel == nil) {
        _tsLabel = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 120, 200, 40)];
        _tsLabel.backgroundColor = [UIColor darkGrayColor];
        _tsLabel.textColor = [UIColor whiteColor];
        _tsLabel.font = [UIFont systemFontOfSize:20];
        _tsLabel.text = @"正向传值";
        _tsLabel.textColor = [UIColor whiteColor];
        _tsLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _tsLabel;
}
//UILabel的懒加载
- (UILabel *)btLabel
{
    if (_btLabel == nil) {
        _btLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, (self.tsLabel.frame.origin.y+self.tsLabel.frame.size.height)+50, 150, 40)];
        _btLabel.backgroundColor = [UIColor grayColor];
        _btLabel.textColor = [UIColor whiteColor];
        _btLabel.font = [UIFont systemFontOfSize:20];
        _btLabel.text = @"发送传值内容：";
    }
    return _btLabel;
}

//UITextField的懒加载
- (UITextField *)textF
{
    if (_textF == nil) {
        _textF = [[UITextField alloc]initWithFrame:CGRectMake((self.btLabel.frame.origin.x+self.btLabel.frame.size.width), (self.tsLabel.frame.origin.y+self.tsLabel.frame.size.height)+50, 200, 40)];
        _textF.textColor = [UIColor blackColor];
        _textF.borderStyle = UITextBorderStyleLine;
//        _textF.backgroundColor = [UIColor grayColor];
        _textF.placeholder = @"请输入内容";
    }
    return _textF;
}

//UIButton的懒加载
- (UIButton *)btn
{
    if (_btn == nil) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.backgroundColor = [UIColor redColor];
        _btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _textF.frame.origin.y+_textF.frame.size.height+40, 200, 40);
        [_btn setTitle:@"跳转B页面" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //点击事件
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
//UILabel的懒加载
- (UILabel *)tsLabel_01
{
    if (_tsLabel_01 == nil) {
        _tsLabel_01 = [[UILabel alloc]init];
//        _tsLabel_01.backgroundColor = [UIColor darkGrayColor];
        _tsLabel_01.textColor = [UIColor redColor];
        _tsLabel_01.text = @"* 注：\n\t 由于是模态跳转页面，所以只能正向传值，不能方向传值，因为反向传值在模态退出后，A页面的每一个生命周期都不会被触发，从而不能获取单例类属性传值，目前我没有找到在模态下可以反向传值的方法，如果你有相应的方法会，你可以在简书上给我留言：\n https://www.jianshu.com/p/271e83234273\n网址到代码里复制！";
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};

        CGSize labelSize = [_tsLabel_01.text boundingRectWithSize:CGSizeMake(200, 5000) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
        _tsLabel_01.frame = CGRectMake(10, (_btn.frame.origin.y+_btn.frame.size.height)+20,self.view.frame.size.width-20, labelSize.height+70);
        _tsLabel_01.numberOfLines = 0;
        _tsLabel_01.lineBreakMode = NSLineBreakByWordWrapping;
        _tsLabel_01.font = [UIFont systemFontOfSize:20];
        _tsLabel_01.textAlignment = NSTextAlignmentLeft;
    }
    return _tsLabel_01;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //把UILabel、UIButton添加到view视图中_btLabel
    [self.view addSubview:self.tsLabel];
    [self.view addSubview:self.btLabel];
    [self.view addSubview:self.textF];
    [self.view addSubview:self.btn];
    [self.view addSubview:self.tsLabel_01];
}

//button点击事件 -- 跳转到页面二
- (void)btnClick
{
    //创建B页面指针，并初始化
    B_2_ViewController *b_2_VC = [[B_2_ViewController alloc]init];
    b_2_VC.view.backgroundColor = [UIColor whiteColor];
    /*
     单例传值 - 正向传递
     通过单例类向B页面传值
     */
    [DefaultInstance shatredInstance].str = _textF.text;
    //跳转下级页面
    [self presentViewController:b_2_VC animated:YES completion:nil];
}

@end
